package main

import "fmt"

/**
  稀疏数组案例
*/
type Note struct {
	row int
	col int
	val int
}

func main() {
	//1.创建一个原始的数组
	var chessMap [11][11]int
	chessMap[1][2] = 1 //黑子
	chessMap[2][3] = 2 //白子
	fmt.Println("-----------------原始数组--------------------")
	//2.输出原始的数组
	for _, v := range chessMap {
		for _, v2 := range v {
			fmt.Printf("%d \t", v2)
		}
		fmt.Println()
	}
	//3. 转成稀疏数组
	//3.1  遍历chessMap，如果有元素不为0，则创建一个 note 节点
	//3.2  将节点保持到集合里

	var sparseList []Note //创建切片

	sparseList = append(sparseList, Note{
		row: 11,
		col: 11,
		val: 0,
	})
	fmt.Println("-----------------稀疏数组--------------------")
	for i, v := range chessMap {
		for j, v2 := range v {
			if v2 != 0 {
				sparseList = append(sparseList, Note{
					row: i,
					col: j,
					val: v2,
				})
			}
		}
	}

	//输出稀疏数组
	for i, note := range sparseList {
		fmt.Printf("%d:\t%d\t%d\t%d\t\n", i, note.row, note.col, note.val)
	}

	fmt.Println("-----------------稀疏数组 恢复原始数组--------------------")
	var chessMap2 [11][11]int
	for i, valNote := range sparseList {
		if i == 0 {
			continue
		}
		chessMap2[valNote.row][valNote.col] = valNote.val
	}

	for _, v1 := range chessMap2 {
		for _, v2 := range v1 {
			fmt.Printf("%d \t", v2)
		}
		fmt.Println()
	}
}
